<?php
function echoTdsRow($team, $row, & $totalFor, & $averageFor) {
    echo getEchoTR($row, "center");
    echo "  <td>" . getEchoValue($team->coach->getName()) . "</td>\n";
    echo "  <td>" . $team->getRosterLink() . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->tdFor) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->tdAg) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getNetTds()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageScore()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageConceded()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageNetTD()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->highTdFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->lowOppoTdFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->lowTdFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->highOppoTdFor->myValue) . "</td>\n";
    echo "</tr>\n";
    $totalFor += $team->currentSeasonStats->tdFor;
    $averageFor += $team->currentSeasonStats->getAverageScore();
}

function echoCasRow($team, $row, & $totalFor, & $averageFor) {
    echo getEchoTR($row, "center");
    echo "  <td>" . getEchoValue($team->coach->getName()) . "</td>\n";
    echo "  <td>" . $team->getRosterLink() . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->casFor) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->casAg) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getNetCas()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageInflicted()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageSuffered()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->getAverageNetCas()) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->highCasFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->lowOppoCasFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->lowCasFor->myValue) . "</td>\n";
    echo "  <td>" . getEchoValue($team->currentSeasonStats->highOppoCasFor->myValue) . "</td>\n";
    echo "</tr>\n";
    $totalFor += $team->currentSeasonStats->casFor;
    $averageFor += $team->currentSeasonStats->getAverageInflicted();
}

function echoWinningsRow($team, $winning, $row) {
    echo getEchoTR($row, "center");
    echo "  <td>" . getEchoValue($team->coach->getName()) . "</td>\n";
    echo "  <td>" . $team->getRosterLink() . "</td>\n";
    echo "  <td>" . getEchoValue($winning->total) . "</td>\n";
    echo "  <td>" . getEchoValue($winning->average) . "</td>\n";
    echo "  <td>" . getEchoValue($winning->high) . "</td>\n";
    echo "  <td>" . getEchoValue($winning->low) . "</td>\n";
    echo "</tr>\n";
}

$showStats = true;
include_once ("includes/header.php");

$sort = getParam("sort");
switch ($sort) {
    case 1 :
        uasort($teams->teams, "totalTdScoredSort");
        break;
    case 2 :
        uasort($teams->teams, "totalTdConcededSort");
        break;
    case 3 :
        uasort($teams->teams, "totalNetTdsSort");
        break;
    case 4 :
        uasort($teams->teams, "totalCasScoredSort");
        break;
    case 5 :
        uasort($teams->teams, "totalCasConcededSort");
        break;
    case 6 :
        uasort($teams->teams, "totalNetCasSort");
        break;
    case 7 :
        uasort($teams->teams, "aveTdScoredSort");
        break;
    case 8 :
        uasort($teams->teams, "aveTdConcededSort");
        break;
    case 9 :
        uasort($teams->teams, "aveNetTdsSort");
        break;
    case 10 :
        uasort($teams->teams, "aveCasScoredSort");
        break;
    case 11 :
        uasort($teams->teams, "aveCasConcededSort");
        break;
    case 12 :
        uasort($teams->teams, "aveNetCasSort");
        break;
    case 13 :
        uasort($teams->teams, "bestTDScoredSort");
        break;
    case 14 :
        uasort($teams->teams, "bestTDConcededSort");
        break;
    case 15 :
        uasort($teams->teams, "bestCasScoredSort");
        break;
    case 16 :
        uasort($teams->teams, "bestCasConcededSort");
        break;
    case 17 :
        uasort($teams->teams, "worstTDScoredSort");
        break;
    case 18 :
        uasort($teams->teams, "worstTDConcededSort");
        break;
    case 19 :
        uasort($teams->teams, "worstCasInflictedSort");
        break;
    case 20 :
        uasort($teams->teams, "worstCasSufferedSort");
        break;
    default :
        uasort($teams->teams, "leagueSort");
        break;
}

$winnings = new Winnings($season);
switch ($sort) {
    case 21 :
        uasort($winnings->winnings, "totalWinningsSort");
        break;
    case 22 :
        uasort($winnings->winnings, "averageWinningsSort");
        break;
    case 23 :
        uasort($winnings->winnings, "highWinningsSort");
        break;
    case 24 :
        uasort($winnings->winnings, "lowWinningsSort");
        break;
    default :
        uasort($winnings->winnings, "averageWinningsSort");
        break;
}

switch ($sort) {
    case 25 :
        uasort($winnings->gates, "totalWinningsSort");
        break;
    case 26 :
        uasort($winnings->gates, "averageWinningsSort");
        break;
    case 27 :
        uasort($winnings->gates, "highWinningsSort");
        break;
    case 28 :
        uasort($winnings->gates, "lowWinningsSort");
        break;
    default :
        uasort($winnings->gates, "averageWinningsSort");
        break;
}
// Touchdownss
echo<<< EOQ
            <caption><h1>Team Stats</h1></caption>
            <tr align="center">
              <th colspan="12"><a name="tds">Touch downs</a></th>
            </tr>\n
            <tr align="center">
              <th rowspan="2">Coach</th>
              <th rowspan="2">Team</th>
              <th colspan="3">Total</th>
              <th colspan="3">Average</th>
              <th colspan="2">Best</th>
              <th colspan="2">Worst</th>
            </tr>
            <tr align="center">
              <th><a href="./stats.php?sort=1#tds">For</a></th>
              <th><a href="./stats.php?sort=2#tds">Ag</a></th>
              <th><a href="./stats.php?sort=3#tds">Net</a></th>
              <th><a href="./stats.php?sort=7#tds">For</a></th>
              <th><a href="./stats.php?sort=8#tds">Ag</a></th>
              <th><a href="./stats.php?sort=9#tds">Net</a></th>
              <th><a href="./stats.php?sort=13#tds">For</a></th>
              <th><a href="./stats.php?sort=14#tds">Ag</a></th>
              <th><a href="./stats.php?sort=17#tds">For</a></th>
              <th><a href="./stats.php?sort=18#tds">Ag</a></th>
            </tr>\n
EOQ;

$i = 1;
$totalFor = 0;
$averageFor = 0;
foreach ($teams->teams as $team) {
    echoTDsRow($team, $i++, $totalFor, $averageFor);
}
echo getEchoTR($i++, "center");
$numTeams = sizeof($teams->teams);
if ($numTeams < 1) {
    $numTeams = 1;
}

$totalFor = round($totalFor / $numTeams, 2);
$averageFor = round($averageFor / $numTeams, 2);

echo<<< EOQ
              <td colspan="2">Average</td>
              <td colspan="3">$totalFor</td>
              <td colspan="3">$averageFor</td>
              <td colspan="4">&nbsp;</td>
            </tr>
            <tr align="center">
              <th colspan="12"><a name="cas"><br><br>Casualties</a></th>
            </tr>\n
            <tr align="center">
              <th rowspan="2">Coach</th>
              <th rowspan="2">Team</th>
              <th colspan="3">Total</th>
              <th colspan="3">Average</th>
              <th colspan="2">Best</th>
              <th colspan="2">Worst</th>
            </tr>
            <tr align="center">
              <th><a href="./stats.php?sort=4#cas">For</a></th>
              <th><a href="./stats.php?sort=5#cas">Ag</a></th>
              <th><a href="./stats.php?sort=6#cas">Net</a></th>
              <th><a href="./stats.php?sort=10#cas">For</a></th>
              <th><a href="./stats.php?sort=11#cas">Ag</a></th>
              <th><a href="./stats.php?sort=12#cas">Net</a></th>
              <th><a href="./stats.php?sort=15#cas">For</a></th>
              <th><a href="./stats.php?sort=16#cas">Ag</a></th>
              <th><a href="./stats.php?sort=19#cas">For</a></th>
              <th><a href="./stats.php?sort=20#cas">Ag</a></th>
            </tr>\n
EOQ;

$i = 1;
$totalFor = 0;
$averageFor = 0;
foreach ($teams->teams as $team) {
    echoCasRow($team, $i++, $totalFor, $averageFor);
}
echo getEchoTR($i++, "center");
$totalFor = round($totalFor / $numTeams, 2);
$averageFor = round($averageFor / $numTeams, 2);

// Casualties
echo<<< EOQ
              <td colspan="2">Average</td>
              <td colspan="3">$totalFor</td>
              <td colspan="3">$averageFor</td>
              <td colspan="4">&nbsp;</td>
            </tr>
            <tr align="center">
              <th colspan="12">
                <table border="0" cellPadding="5" cellSpacing="0" width="100%">
                <tr align="center">
                  <th colspan="6"><a name="winnings"><br><br>Winnings</a></th>
                </tr>\n
                <tr align="center">
                  <th>Coach</th>
                  <th>Team</th>
                  <th><a href="./stats.php?sort=21#winnings">Total</a></th>
                  <th><a href="./stats.php?sort=22#winnings">Average</a></th>
                  <th><a href="./stats.php?sort=23#winnings">High</a></th>
                  <th><a href="./stats.php?sort=24#winnings">Low</a></th>
                </tr>\n
EOQ;

$i = 1;
foreach ($winnings->winnings as $winning) {
    echoWinningsRow($teams->getTeam($winning->teamId), $winning, $i++);
}
echo "</table></th>\n";

// Gates
echo<<< EOQ
            <tr align="center">
              <th colspan="12">
                <table border="0" cellPadding="5" cellSpacing="0" width="100%">
                <tr align="center">
                  <th colspan="6"><a name="gates"><br><br>Gates (000s)</a></th>
                </tr>\n
                <tr align="center">
                  <th>Coach</th>
                  <th>Team</th>
                  <th><a href="./stats.php?sort=25#gates">Total</a></th>
                  <th><a href="./stats.php?sort=26#gates">Average</a></th>
                  <th><a href="./stats.php?sort=27#gates">High</a></th>
                  <th><a href="./stats.php?sort=28#gates">Low</a></th>
                </tr>\n
EOQ;

$i = 1;
foreach ($winnings->gates as $gate) {
    echoWinningsRow($teams->getTeam($gate->teamId), $gate, $i++);
}
echo "</table></th>\n";

include ("includes/footer.php");
?>
